package com.itheima.controller.system;

import com.github.pagehelper.PageInfo;
import com.itheima.controller.BaseServlet;
import com.itheima.domain.system.SysLog;
import com.itheima.service.impl.system.SysLogServiceImpl;
import com.itheima.service.system.SysLogService;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

@WebServlet("/system/syslog/*")
public class SysLogServlet extends BaseServlet {
    private SysLogService syslogService = new SysLogServiceImpl();

    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pageNum = req.getParameter("page");
        String pageSize = req.getParameter("pageSize");
        int pageN = 1;
        int pageS = 7;
        if (pageNum != null) {
            pageN = Integer.parseInt(pageNum);
        }
        if (pageSize != null) {
            pageS = Integer.parseInt(pageSize);
        }

        List<SysLog> list = syslogService.findAll(pageN, pageS);
        PageInfo<SysLog> pageInfo = new PageInfo<>(list);
        req.setAttribute("page", pageInfo);
        req.getRequestDispatcher("/WEB-INF/pages/system/syslog-list.jsp").forward(req, resp);
    }

    protected void deleteById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String ids = req.getParameter("idArr");
        syslogService.delete(ids);
        list(req, resp);
    }


    protected void toExport(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ByteArrayOutputStream os = syslogService.getReport();
        //返回类型为xlsx格式
        resp.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName = new String("综合练习.xlsx".getBytes(), "iso8859-1");
        resp.addHeader("Content-Disposition", "attachment;fileName=" + fileName);
        ServletOutputStream sos = resp.getOutputStream();
        //将数据从原始的字节流中写入servlet对应的输出流中
        os.writeTo(sos);
        sos.flush();
        sos.close();
        os.close();
    }


    protected void findByUsername(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String pageNum = req.getParameter("page");
        String pageSize = req.getParameter("pageSize");
        int pageN = 1;
        int pageS = 7;
        if (pageNum != null) {
            pageN = Integer.parseInt(pageNum);
        }
        if (pageSize != null) {
            pageS = Integer.parseInt(pageSize);
        }
        List<SysLog> list = syslogService.findByUsername(username,pageN,pageS);
        PageInfo<SysLog> pageInfo=new PageInfo<>(list);
        req.setAttribute("page",pageInfo);

        req.getRequestDispatcher("/WEB-INF/pages/system/syslog-list.jsp").forward(req,resp);

    }
}